package com.imooc.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author liansen
 * @create 02-22-14:23
 */
@Slf4j
@RestController
public class RabbitMQSMSController_DEAD {

    @RabbitListener(queues = {RabbitmqDeadConfig.SMS_QUEUE_DEAD})
    public void watchQueue(String content, Message message, Channel channel) throws IOException {

        try {
            String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
            log.info("receivedRoutingKey" + receivedRoutingKey);

            //获取到死信队列中的消息并消费(输出)
            System.out.println("死信队列收到content" + content);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        }catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, false);
        }
    }
}
